package com.lovo.psc.controller;

import com.alibaba.fastjson.JSON;
import com.lovo.commom.util.ImageysUtils;
import com.lovo.commom.util.MD5Encryptor;
import com.lovo.commom.vo.ResultVO;
import com.lovo.psc.bean.VerdorBean;
import com.lovo.psc.service.IVerdorService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("verdor")
public class VerdorController {
    @Autowired
    IVerdorService verdorService;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @RequestMapping("replenishUserTwoInfo")
    public ResultVO replenishUserInfo(VerdorBean verdorBean) throws NoSuchAlgorithmException {
        ResultVO resultVO = new ResultVO();
        Boolean aBoolean = verdorService.replenishUserInfo(verdorBean);
        if (aBoolean) {
            resultVO.setVerdorTag(1);
            resultVO.setJson("注册成功,审核中");
        } else {
            resultVO.setJson("用户名已存在");
            resultVO.setVerdorTag(2);
        }
        return resultVO;
    }
    @RequestMapping("again")
    public ResultVO again(VerdorBean verdorBean) {
        ResultVO resultVO = new ResultVO();
        verdorService.again(verdorBean);
        resultVO.setVerdorTag(1);
        resultVO.setJson("注册成功,审核中");

        return resultVO;
    }

    @GetMapping("login")
    public ResultVO login(String name, String pwd) throws NoSuchAlgorithmException {
        VerdorBean verdorBean = verdorService.login(name, MD5Encryptor.encrypt(pwd));
        ResultVO resultVO = new ResultVO();
        if (verdorBean != null) {
            //把对象转换成json
            String json = JSON.toJSONString(verdorBean);
//            //生成Token
//            String token = CreateToken.createToken(json);
            resultVO.setTag(0);
            resultVO.setJson(json);
        } else {
            resultVO.setTag(1);
            resultVO.setJson("");
        }
        return resultVO;
    }

    @RequestMapping("findByName")
    public VerdorBean findByName(String name) {
        return verdorService.findByName(name);
    }

    @RequestMapping("updateStatus/{vendorName}/{status}/{cause}")
    public boolean updateStatus(@PathVariable("vendorName") String vendorName, @PathVariable("status") int status, @PathVariable("cause") String cause) {
        verdorService.updateStatus(vendorName, status, cause);
        return true;
    }
}
